package com.example.qts.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qts.R;
import com.example.qts.bean.User;

import org.w3c.dom.Text;

import java.util.List;

public class QSLadapter extends RecyclerView.Adapter<QSLadapter.ViewHolder> {
    private Context context;
    private List<User> datas;
    private OnContextClickListener onContextClickListener;
    public interface OnContextClickListener{
        void oncontextclicklistener(int position);
    }

    public void setOnContextClickListener(OnContextClickListener onContextClickListener) {
        this.onContextClickListener = onContextClickListener;
    }

    public QSLadapter(Context context, List<User> datas) {
        this.context = context;
        this.datas = datas;
    }

    @NonNull
    @Override
    public QSLadapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.sqlitem, null, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull QSLadapter.ViewHolder holder, int position) {
        User user = datas.get(position);
        holder.te.setText(user.getId()+"");
        holder.te1.setText(user.getName());
        holder.te2.setText(user.getAge()+"");
        holder.te3.setText(user.getSex()+"");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onContextClickListener.oncontextclicklistener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView te;
        TextView te1;
        TextView te2;
        TextView te3;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            te=itemView.findViewById(R.id.te);
            te1=itemView.findViewById(R.id.te1);
            te2=itemView.findViewById(R.id.te2);
            te3=itemView.findViewById(R.id.te3);

        }
    }
}
